package by.semerikov.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import by.semerikov.entity.CarEntity;
import by.semerikov.exceptions.DAOException;

import com.mysql.jdbc.PreparedStatement;

public class CarDAO implements ICarDAO {
	
	private Connection connection;
	
	PreparedStatement insertStatement;
	
	public CarDAO() {
	}

	public CarDAO(Connection con) {
		this.connection = con;
	}
	
	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}


	@Override
	public void create(CarEntity object) throws DAOException {
		try {
			PreparedStatement preparedState = (PreparedStatement) connection
					.prepareStatement("insert into transportsystem.cars (model,state,personal_id) values(?,?,?)");
			preparedState.setString(1, object.getModel());
			preparedState.setString(2, object.getState());
			preparedState.setInt(3, object.getOwnerId());
			preparedState.executeUpdate();
		} catch (Exception e) {
			throw new DAOException("exception.dao.car.create" ,e );
		}
		
	}

	@Override
	public List<CarEntity> read(CarEntity object) throws DAOException {
		ArrayList<CarEntity> newList = new ArrayList<CarEntity>();
		try {
			PreparedStatement preparedState = doReadPreparedStatement(object,
					"SELECT * FROM transportsystem.cars ");
			ResultSet resultset = preparedState.executeQuery();
			while (resultset.next()) {
 				newList.add(new CarEntity(Integer.parseInt(resultset
						.getString("id")), resultset.getString("model"),
						resultset.getString("state"), resultset.getInt("personal_id")));
			}
			return newList;
		} catch (Exception e) {
			throw new DAOException("exception.dao.car.read",e);
		}
	}
	
	private PreparedStatement doReadPreparedStatement(CarEntity object,
			String begin) throws SQLException {
		String newStr = begin;
		Map<String, Integer> map = new HashMap<String, Integer>();
		int number = 1;
		if (object.getId() != null) {
			newStr += "where  id = ?";
			map.put("id", number);
			number++;
		}
		if (object.getModel() != null) {
			if (number == 1)
				newStr += " where ";
			if (number != 1)
				newStr += " And ";
			newStr += " model Like ?";
			map.put("model", number);
			number++;
		}
		if (object.getState() != null) {
			if (number == 1)
				newStr += " where ";
			if (number != 1)
				newStr += " And ";
			newStr += " state Like ?";
			map.put("state", number);
			number++;
		}
		if (object.getOwnerId() != null) {
			if (number == 1)
				newStr += " where ";
			if (number != 1)
				newStr += " And ";
			newStr += " personal_id = ?";
			map.put("personal_id", number);
			number++;
		}
		PreparedStatement newPS = (PreparedStatement) connection
				.prepareStatement(newStr);
		if (map.get("id") != null) {
			newPS.setInt(map.get("id"), object.getId());
		}
		if (map.get("model") != null) {
			newPS.setString(map.get("model"), "%" + object.getModel() + "%");
		}
		if (map.get("state") != null) {
			newPS.setString(map.get("state"), "%" + object.getState() + "%");
		}
		if (map.get("personal_id") != null) {

			newPS.setInt(map.get("personal_id"), object.getOwnerId()  );
		}
		return newPS;
	}

	@Override
	public void update(CarEntity object) throws DAOException {
		try {
			PreparedStatement preparedState = (PreparedStatement) connection
					.prepareStatement("UPDATE transportsystem.cars SET model=? , state=? , personal_id=?  WHERE id=?");
			preparedState.setString(1, object.getModel());
			preparedState.setString(2, object.getState());
			preparedState.setInt(3, object.getOwnerId());
			preparedState.setInt(4, object.getId());
			preparedState.executeUpdate();
		} catch (Exception e) {
			throw new DAOException("exception.dao.car.update",e);
		}
		
	}

	@Override
	public void delete(CarEntity object) throws DAOException {
		try {
			PreparedStatement preparedState = doReadPreparedStatement(object,
					"DELETE FROM transportsystem.cars ");
			preparedState.executeUpdate();
		} catch (Exception e) {
			throw new DAOException("exception.dao.car.delete",e);
		}
	}


}
